<template>
	<view class="">
		<view class="list">
			<view class="school-item" v-for="(item,index) in list" :key="index">
				<view class="school-title">{{item.name}}</view>
				<view class="students-btn" @click="chooseStudents(item)" v-if="item.is_bind == 0">确认绑定</view>
				<view class="students-btn1" v-else>已绑定</view>
				<!-- <u-icon name="checkmark" v-if="item.isSelect" color="#FC6A84" size="20"></u-icon> -->
			</view>
		</view>
		<!-- <view style="padding: 30rpx;">
			<view class="add-students-view" @click="goToUrl">
				<u-icon name="plus-circle" color="#FC6A84" size="20"></u-icon>
				<view class="add-students-title">添加孩子</view>
			</view>
		</view> -->
		<view v-if="list.length == 0">
			<u-empty mode="list">
			</u-empty>
		</view>
		
		<!-- 底部操作栏 -->
		<view style="height: 200rpx;"></view>
		<view class="school-bottom-view">
			<view class="school-bootom-btn" @click="goToUrl">
				添加孩子
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '', // 学生姓名
				identity_card:'',// 身份证	
				list:[],
				student_id:[],// 学生id
			}
		},
		onShow() {
			if(uni.getStorageSync('student_list')){
				this.list = uni.getStorageSync('student_list')
				this.list = this.list.map(item=>{
					return {
						id:item.id,
						name:item.name,
						isSelect:false,
						is_bind:item.is_bind
					}
				})
			}
		},
		methods:{
			// 选择学生
			chooseStudents(item){
				if(this.student_id.length == 0){
					this.student_id.push(item)
					console.log(this.student_id)
					uni.setStorageSync('student_id',this.student_id)
				}else{
					let arr = []
					arr = this.student_id.filter(v=>{
						return v.id == item.id
					})
					if(arr.length > 0){
						this.student_id = this.student_id.filter(v=>{
							return v.id !== item.id
						})
						this.student_id.push(item)
						console.log(this.student_id)
						uni.setStorageSync('student_id',this.student_id)
					}else{
						this.student_id.push(item)
						console.log(this.student_id)
						uni.setStorageSync('student_id',this.student_id)
					}
				}
				this.list.forEach((v,i)=>{
					if(v.id == item.id){
						this.list[i].is_bind = 1
					}
				})
				uni.setStorageSync('student_list',this.list)
				this.$api.post(global.apiUrls.bingUser, {
					student_id: item.id
				}).then(res => {
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
				})
			},
			// 跳转到搜索学生列表
			goToUrl(){
				uni.navigateTo({
					url:'/pages/user/user/info/student'
				})
			},
			// 绑定学生
			handlebindUser(item){
				this.$api.post(global.apiUrls.bingUser, {
					student_id: item.id
				}).then(res => {
					uni.showToast({
						title:res.data.msg,
						icon:'none'
					})
					uni.setStorageSync('student_id',this.student_id)
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.students-btn1{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 20rpx;
		background-color: #f4f4f4;
		border-radius: 10rpx;
		font-size: 13px;
		color: #999999;
	}
	.students-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 10rpx 20rpx;
		background-color: #FC6A84;
		border-radius: 10rpx;
		font-size: 13px;
		color: #ffffff;
	}
	.add-students-title{
		font-size: 15px;
		color: #000000;
		margin-left: 20rpx;
	}
	.add-students-view{
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx;
	}
	.header-search-btn1{
		width: 150rpx;
		height: 60rpx;
		font-size: 15px;
		color: #999999;
		background-color: #E4E7ED;
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
		margin-right: 20rpx;
	}
	.header-search-btn{
		width: 150rpx;
		height: 60rpx;
		font-size: 15px;
		color: #ffffff;
		background-color: #FC6A84;
		border-radius: 10rpx;
		text-align: center;
		line-height: 60rpx;
	}
	.header-search-btnview{
		display: flex;
		align-items: center;
		flex-direction: row-reverse;
		margin-top: 30rpx;
	}
	.school-bootom-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 15px;
		color: #ffffff;
		padding: 20rpx;
		background-color: #FC6A84;
		border-radius: 50rpx;
	}
	.school-bottom-view{
		width: 100%;
		position: fixed;
		bottom: 0rpx;
		left: 0;
		padding: 30rpx;
		background-color: #ffffff;
	}
	.school-title {
		font-size: 15px;
		color: #000000;
	}

	.school-item:last-child {
		margin-bottom: 0;
	}

	.school-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 30rpx;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx;
	}

	.list {
		padding: 30rpx;
	}

	.school-search-header {
		background-color: #ffffff;
		padding: 30rpx;
	}
</style>